#include "dialogmc.h"
#include "ui_dialogmc.h"
#include <QImage>
#include <QPainter>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QFileDialog>
#include <QFileInfo>
#include <QSettings>
#include <QVector>

const int pic_width = 180, pic_height = 40 , char_words = 5;
const char vch [] = "23456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int total_chars = strlen(vch);
DialogMC::DialogMC(QWidget *parent) :
	QDialog(parent),
	ui(new Ui::DialogMC),
	m_process(new QProcess(this))
{
	ui->setupUi(this);
	loadIni();
	connect (m_process, &QProcess::readyReadStandardError,this,&DialogMC::slot_readyReadOutput);
	connect (m_process, &QProcess::readyReadStandardOutput,this,&DialogMC::slot_readyReadErr);
#if QT_VERSION >=0x060000
	connect (m_process, &QProcess::finished,this,&DialogMC::slot_finished);
#else
	connect (m_process, static_cast<void (QProcess::*)(int)>(&QProcess::finished),this,&DialogMC::slot_finished);
#endif
	connect (m_process, &QProcess::started,this,&DialogMC::slot_started);
}

DialogMC::~DialogMC()
{
	delete ui;
}

void DialogMC::on_pushButton_ok_clicked()
{
	saveIni();
	const int tms = ui->spinBox_sps->value();
	const int start_tm = ui->spinBox_start->value();
	const QString mainFeaDir = ui->lineEdit_TFeatureDir->text();
	const QString rect_feature = mainFeaDir + "/split";
	const QString char_feature = mainFeaDir + "/char";
	QDir dir;
	dir.mkpath(rect_feature);
	dir.mkpath(char_feature);

	for(int tt=0;tt<tms;++tt)
	{

		QString fm_split = QString("%1/%2").arg(rect_feature).arg(tt+start_tm);
		QFile fp;
		fp.setFileName(fm_split+".scalar");
		if (fp.open(QIODevice::WriteOnly))
		{
			double s = 0;
			fp.write((char *)&s,sizeof(s));
			fp.close();
		}

		QImage img(pic_width,pic_height,QImage::Format_RGB888);
		//QColor cBk(rand()%32+168,rand()%64+128,rand()%54+128);
		QColor cBk(255,255,255);
		img.fill(cBk);
		QPainter painter(&img);

		QVector<QRectF> bounds;
		QVector<int> charids;
		const int lines =  rand()%64+1;
		for (int j=0;j<lines;j++)
		{
			QPen pl = QPen(QBrush(QColor(rand()%64+128,rand()%64+128,rand()%16+16,192)),rand()%4);

			painter.setPen(pl);

			const int x1 = rand()% pic_width;
			const int y1 = rand()% pic_height;
			const int x2 = x1 + rand()% 64 - 32;
			const int y2 = y1 + rand()% 64 - 32;
			if (j<4)
				painter.drawLine(x1,y1,x2,y2);
			else
				painter.drawPoint(x1,y1);
		}
		//绘制文字
		for (int j=0;j<char_words;++j)
		{
			const char * fontfm[] = {
				"DejaVu Serif","Microsoft Sans Serif","Courier New","Trebuchet MS"
			};
			QFont serifFont(fontfm[rand()%4], rand()%12+20, QFont::Normal);
			painter.setFont(serifFont);
			painter.setPen(QColor(rand()%32+32,rand()%64,rand()%64));
			QString str;
			const int cid = rand() % total_chars;
			charids.push_back(cid);
			str.push_back(vch[cid]);

			const double seed = pic_width / (char_words+0.3);
			//计算中心
			const int center_x = j*seed+seed*0.75+rand() % 20 - 10,  center_y = pic_height/2 + rand() % 8 -4;
			//qDebug()<<center_x<<","<<center_y;
			painter.translate(center_x,center_y);
			painter.rotate(rand() % 30 - 15);                    // 第2步： 旋转一定角度
			QRectF brec;
			painter.drawText(QRectF(-seed/2,-pic_height/2,seed,pic_height), Qt::AlignCenter, str,&brec);      // 第3步： 绘制文字
			//qDebug()<<brec;
			painter.resetTransform();                  // 第4步： 恢复坐标旋转
			painter.setBrush(Qt::NoBrush);
			brec.setX(brec.x()-4);
			brec.setWidth(brec.width()+2);
			brec.translate(center_x,center_y);
			painter.drawRect(brec);
			bounds.push_back(brec);

		}

		img.save(fm_split+".png");
		//Output File
		QVector<double> dr,dc;
		fp.setFileName(fm_split+".2d");
		if (fp.open(QIODevice::WriteOnly))
		{
			for (int i=0;i<pic_width;++i)
			{
				dr.push_back(0);
				dr.push_back(0);
				dr.push_back(0);
			}
			for (int i=0;i<pic_height;++i)
			{
				dc.push_back(0);
				dc.push_back(0);
				dc.push_back(0);
			}
			for (int i=0;i<pic_width;++i)
			{
				for (int j=0;j<pic_height;++j)
				{
					QRgb r = img.pixel(i,j);
					double s = (unsigned int)r & 0xff;
					s /=255.0;
					fp.write((char *)&s,sizeof(s));
					dr[i*3+0] += s/pic_height;
					dc[j*3+0] += s/pic_width;
					s = (unsigned int)(r>>8) & 0xff;
					s /=255.0;
					dr[i*3+1] += s/pic_height;
					dc[j*3+1] += s/pic_width;
					fp.write((char *)&s,sizeof(s));
					s = (unsigned int)(r>>16) & 0xff;
					s /=255.0;
					dr[i*3+2] += s/pic_height;
					dc[j*3+2] += s/pic_width;
					fp.write((char *)&s,sizeof(s));
				}
			}
			fp.close();
		}
		fp.setFileName(fm_split+".1d");
		if (fp.open(QIODevice::WriteOnly))
		{
			if (dr.size())
				fp.write((char *)dr.constData(),dr.size()*sizeof(double));
			if (dc.size())
				fp.write((char *)dc.constData(),dc.size()*sizeof(double));
			fp.close();
		}
		fp.setFileName(fm_split+".train");
		if (fp.open(QIODevice::WriteOnly))
		{
			for (int i=0;i<char_words;++i)
			{
				double s = bounds[i].x()/pic_width;
				fp.write((char *)&s,sizeof(double));
				s = bounds[i].y()/pic_height;
				fp.write((char *)&s,sizeof(double));
				s = (bounds[i].x()+bounds[i].width()-1)/pic_width;
				fp.write((char *)&s,sizeof(double));
				s = (bounds[i].y()+bounds[i].height()-1)/pic_height;
				fp.write((char *)&s,sizeof(double));
			}
			fp.close();
		}

		if (ui->checkBox_makeChar->isChecked()==true)
		{
			//单个字符
			for (int cha=0;cha<char_words;++cha)
			{
				dr.clear();
				dc.clear();
				const int resp_width=48, resp_height = 32;
				QString fm_char = QString("%1/%2").arg(char_feature).arg((tt+start_tm)*4+cha);
				fp.setFileName(fm_char+".scalar");
				if (fp.open(QIODevice::WriteOnly))
				{
					double s = 0;
					fp.write((char *)&s,sizeof(s));
					fp.close();
				}
				//提取单个
				fp.setFileName(fm_char+".2d");
				//单个字符全部采样到一样大小，即 resp_width resp_height
				if (fp.open(QIODevice::WriteOnly))
				{
					for (int i=0;i<resp_width;++i)
					{
						dr.push_back(0);
						dr.push_back(0);
						dr.push_back(0);
					}
					for (int i=0;i<resp_height;++i)
					{
						dc.push_back(0);
						dc.push_back(0);
						dc.push_back(0);
					}
					for (int i=0;i<resp_width;++i)
					{
						for (int j=0;j<resp_height;++j)
						{
							//计算坐标
							const double xratio = i * 1.0 / resp_width;
							const double yratio = j * 1.0 / resp_height;
							const double rx = bounds[cha].x() + xratio * bounds[cha].width()+.5;
							const double ry = bounds[cha].y() + yratio * bounds[cha].height()+.5;
							//最近邻
							const int nx = rx;
							const int ny = ry;
							QRgb r = 0xffffffff;
							if (nx >= 0 && nx < img.width() && ny >=0 && ny<img.height())
								r = img.pixel(nx,ny);
							double s = (unsigned int)r & 0xff;
							s /=255.0;
							fp.write((char *)&s,sizeof(s));
							dr[i*3+0] += s/resp_height;
							dc[j*3+0] += s/resp_width;
							s = (unsigned int)(r>>8) & 0xff;
							s /=255.0;
							dr[i*3+1] += s/resp_height;
							dc[j*3+1] += s/resp_width;
							fp.write((char *)&s,sizeof(s));
							s = (unsigned int)(r>>16) & 0xff;
							s /=255.0;
							dr[i*3+2] += s/resp_height;
							dc[j*3+2] += s/resp_width;
							fp.write((char *)&s,sizeof(s));
						}
					}
					fp.close();
				}
				fp.setFileName(fm_char+".1d");
				if (fp.open(QIODevice::WriteOnly))
				{
					if (dr.size())
						fp.write((char *)dr.constData(),dr.size()*sizeof(double));
					if (dc.size())
						fp.write((char *)dc.constData(),dc.size()*sizeof(double));
					fp.close();
				}
				fp.setFileName(fm_char+".train");
				if (fp.open(QIODevice::WriteOnly))
				{
					const int chars = total_chars;
					for (int i=0;i<chars;++i)
					{
						double s = charids[cha]==i?1:0;
						fp.write((char *)&s,sizeof(double));
					}
					fp.close();
				}

			}
		}//end if (ui->checkBox_makeChar->isChecked()==true)

		ui->label_pic->setPixmap(QPixmap::fromImage(img));
		ui->spinBox_sps->setValue(ui->spinBox_sps->value()-1);
		QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);

	}



}

void DialogMC::on_pushButton_run_clicked()
{
	saveIni();
	const int totalchars = total_chars;
	QImage img;
	if (img.load(ui->lineEdit_pic->text())==false)
		return;

	QFileInfo fpImg(ui->lineEdit_pic->text());
	const QString abdir = fpImg.absolutePath();
	const QString feaSplitDir = abdir+"/"+fpImg.completeBaseName()+"/Split";
	const QString feaCharDir = abdir+"/"+fpImg.completeBaseName()+"/Char";
	QDir dir;
	if (false==dir.mkpath(feaSplitDir))
		return ;
	if (false==dir.mkpath(feaCharDir))
		return;

	const QString fm_split = feaSplitDir+"/"+fpImg.completeBaseName();
	QFile fp;
	fp.setFileName(fm_split+".scalar");
	if (fp.open(QIODevice::WriteOnly))
	{
		double s = 0;
		fp.write((char *)&s,sizeof(s));
		fp.close();
	}
	QVector<QRectF> bounds;
	QVector<int> charids;
	//绘制文字
	QVector<double> dr,dc;
	fp.setFileName(fm_split+".2d");
	if (fp.open(QIODevice::WriteOnly))
	{
		for (int i=0;i<pic_width;++i)
		{
			dr.push_back(0);
			dr.push_back(0);
			dr.push_back(0);
		}
		for (int i=0;i<pic_height;++i)
		{
			dc.push_back(0);
			dc.push_back(0);
			dc.push_back(0);
		}
		for (int i=0;i<pic_width;++i)
		{
			for (int j=0;j<pic_height;++j)
			{
				QRgb r = img.pixel(i,j);
				double s = (unsigned int)r & 0xff;
				s /=255.0;
				fp.write((char *)&s,sizeof(s));
				dr[i*3+0] += s/pic_height;
				dc[j*3+0] += s/pic_width;
				s = (unsigned int)(r>>8) & 0xff;
				s /=255.0;
				dr[i*3+1] += s/pic_height;
				dc[j*3+1] += s/pic_width;
				fp.write((char *)&s,sizeof(s));
				s = (unsigned int)(r>>16) & 0xff;
				s /=255.0;
				dr[i*3+2] += s/pic_height;
				dc[j*3+2] += s/pic_width;
				fp.write((char *)&s,sizeof(s));
			}
		}
		fp.close();
	}
	fp.setFileName(fm_split+".1d");
	if (fp.open(QIODevice::WriteOnly))
	{
		if (dr.size())
			fp.write((char *)dr.constData(),dr.size()*sizeof(double));
		if (dc.size())
			fp.write((char *)dc.constData(),dc.size()*sizeof(double));
		fp.close();
	}
	ui->label_pic->setPixmap(QPixmap::fromImage(img));
	QString Output = fm_split + ".result";
	if (m_process->state()==QProcess::NotRunning)
	{
		//call python
		QString cmdline;
		const QString cmd = ui->lineEdit_pythonPath->text();
		cmdline += cmd;
		QStringList args;
		args << ui->lineEdit_NNPath->text() +"/gnn_run_model.py";
		args << "-t";
		args << ui->lineEdit_TBPATH->text();
		args << "-i";
		args << feaSplitDir;
		QFileInfo info(ui->lineEdit_TBPATH->text());
		QString ModelPath = info.absolutePath() + "/" + info.completeBaseName() + "_Saved/Model";
		args << "-m";
		args << ModelPath;

		args << "-o";
		args << Output;

		m_process->setProgram(cmd);
		m_process->setArguments(args);
		foreach (QString c, args)
		{
			if (c.startsWith("-"))
				cmdline += " " + c;
			else
				cmdline += " \"" + c + "\"";
		}
		ui->plainTextEdit_output->appendPlainText(cmdline+"\n");
		m_process->start();


		m_process->waitForFinished();
	}

	//打开文件
	QFile outputfile(Output);
	if (outputfile.open(QIODevice::ReadOnly)==false)
		return;
	double rects[char_words*4];
	if (sizeof(double)*char_words*4!=outputfile.read((char *)rects,sizeof(double)*char_words*4))
		return;
	outputfile.close();

	//绘制矩形
	QPainter painter(&img);
	painter.setBrush(Qt::NoBrush);
	for (int tt=0;tt<char_words;++tt)
	{
		const QString fm_char = feaCharDir+"/"+fpImg.completeBaseName()+QString("_%1").arg(tt);
		bounds.push_back(
					QRectF(rects[0+tt*4]*pic_width,rects[1+tt*4]*pic_height,
				(rects[2+tt*4]-rects[0+tt*4])*pic_width,(rects[3+tt*4]-rects[1+tt*4])*pic_height)
				);
		painter.drawRect(bounds[tt]);

		dr.clear();
		dc.clear();
		const int resp_width=48, resp_height = 32;
		fp.setFileName(fm_char+".scalar");
		if (fp.open(QIODevice::WriteOnly))
		{
			double s = 0;
			fp.write((char *)&s,sizeof(s));
			fp.close();
		}
		//提取单个
		fp.setFileName(fm_char+".2d");
		//单个字符全部采样到一样大小，即 resp_width resp_height
		if (fp.open(QIODevice::WriteOnly))
		{
			for (int i=0;i<resp_width;++i)
			{
				dr.push_back(0);
				dr.push_back(0);
				dr.push_back(0);
			}
			for (int i=0;i<resp_height;++i)
			{
				dc.push_back(0);
				dc.push_back(0);
				dc.push_back(0);
			}
			for (int i=0;i<resp_width;++i)
			{
				for (int j=0;j<resp_height;++j)
				{
					//计算坐标
					const double xratio = i * 1.0 / resp_width;
					const double yratio = j * 1.0 / resp_height;
					const double rx = bounds[tt].x() + xratio * bounds[tt].width()+.5;
					const double ry = bounds[tt].y() + yratio * bounds[tt].height()+.5;
					//最近邻
					const int nx = rx;
					const int ny = ry;
					QRgb r = 0xffffffff;
					if (nx >= 0 && nx < img.width() && ny >=0 && ny<img.height())
						r = img.pixel(nx,ny);
					double s = (unsigned int)r & 0xff;
					s /=255.0;
					fp.write((char *)&s,sizeof(s));
					dr[i*3+0] += s/resp_height;
					dc[j*3+0] += s/resp_width;
					s = (unsigned int)(r>>8) & 0xff;
					s /=255.0;
					dr[i*3+1] += s/resp_height;
					dc[j*3+1] += s/resp_width;
					fp.write((char *)&s,sizeof(s));
					s = (unsigned int)(r>>16) & 0xff;
					s /=255.0;
					dr[i*3+2] += s/resp_height;
					dc[j*3+2] += s/resp_width;
					fp.write((char *)&s,sizeof(s));
				}
			}
			fp.close();
		}
		fp.setFileName(fm_char+".1d");
		if (fp.open(QIODevice::WriteOnly))
		{
			if (dr.size())
				fp.write((char *)dr.constData(),dr.size()*sizeof(double));
			if (dc.size())
				fp.write((char *)dc.constData(),dc.size()*sizeof(double));
			fp.close();
		}

	}

	ui->label_pic->setPixmap(QPixmap::fromImage(img));

	if (m_process->state()==QProcess::NotRunning)
	{
		//call python
		QString cmdline;
		const QString cmd = ui->lineEdit_pythonPath->text();
		cmdline += cmd;
		QStringList args;
		args << ui->lineEdit_NNPath->text() +"/gnn_run_model.py";
		args << "-t";
		args << ui->lineEdit_CHPATH->text();
		args << "-i";
		args << feaCharDir;
		QFileInfo info(ui->lineEdit_CHPATH->text());
		QString ModelPath = info.absolutePath() + "/" + info.completeBaseName() + "_Saved/Model";
		args << "-m";
		args << ModelPath;
		QString Output = fm_split + ".result";
		args << "-o";
		args << Output;
		m_process->setProgram(cmd);
		m_process->setArguments(args);
		foreach (QString c, args)
		{
			if (c.startsWith("-"))
				cmdline += " " + c;
			else
				cmdline += " \"" + c + "\"";
		}
		ui->plainTextEdit_output->appendPlainText(cmdline+"\n");
		m_process->start();
		m_process->waitForFinished();
	}

	QString strResult;
	//显示结果
	for (int tt=0;tt<char_words;++tt)
	{
		const QString fm_char = feaCharDir+"/"+fpImg.completeBaseName()+QString("_%1.result").arg(tt);
		outputfile.setFileName(fm_char);
		if (outputfile.open(QIODevice::ReadOnly)==false)
			return;

		double judge[totalchars];
		if (sizeof(double)*totalchars!=outputfile.read((char *)judge,sizeof(double)*totalchars))
			return;
		outputfile.close();

		double * el = std::max_element(judge,judge+totalchars);
		int c = el - judge;
		strResult += vch[c];
	}

	ui->label_result->setText(strResult);
}

void DialogMC::on_toolButton_br_python_clicked()
{
	QString strFileName = getFilePath(tr("Python.exe"),"lastPythonDir","python.exe(python.exe Python.exe);;All files (*.*)",true);
	ui->lineEdit_pythonPath->setText(strFileName);
	saveIni();
}

void DialogMC::on_toolButton_br_NN_clicked()
{
	QString strFileName = QFileDialog::getExistingDirectory(this,tr("CNN pyton Scripts Directory"),ui->lineEdit_NNPath->text(),QFileDialog::ShowDirsOnly);
	ui->lineEdit_NNPath->setText(strFileName);
	saveIni();
}

void DialogMC::on_toolButton_br_splitmodel_clicked()
{
	QString strFileName = getFilePath(tr("Split Model Project"),"lastSplitProjectDir","Project files (*.qnprj *.cfg);;All files (*.*)",true);
	ui->lineEdit_TBPATH->setText(strFileName);
	saveIni();
}

void DialogMC::on_toolButton_br_charmodel_clicked()
{
	QString strFileName = getFilePath(tr("Char Rec Model Project"),"lastCharRecProjectDir","Project files (*.qnprj *.cfg);;All files (*.*)",true);
	ui->lineEdit_CHPATH->setText(strFileName);
	saveIni();
}
void DialogMC::on_toolButton_br_TFeaDir_triggered(QAction *arg1)
{
	QString strFileName = QFileDialog::getExistingDirectory(this,tr("Trainning Features Directory"),ui->lineEdit_TFeatureDir->text(),QFileDialog::ShowDirsOnly);
	ui->lineEdit_TFeatureDir->setText(strFileName);
	saveIni();
}

void DialogMC::on_toolButton_br_TFeaDir_clicked()
{
	QString strFileName = QFileDialog::getExistingDirectory(this,tr("Trainning Features Directory"),ui->lineEdit_TFeatureDir->text(),QFileDialog::ShowDirsOnly);
	ui->lineEdit_TFeatureDir->setText(strFileName);
	saveIni();
}

void DialogMC::on_toolButton_br_test_clicked()
{
	QString strFileName = getFilePath(tr("image"),"lastPictureDir","images(*.png *.jpg *.jpeg *.bmp);;All files (*.*)",true);
	ui->lineEdit_pic->setText(strFileName);
	saveIni();
}
void DialogMC::saveIni()
{
	QSettings settings(QCoreApplication::applicationFilePath()+".ini",QSettings::IniFormat);
	settings.setValue("ui/lineEdit_NNPath",ui->lineEdit_NNPath->text());
	settings.setValue("ui/lineEdit_TBPATH",ui->lineEdit_TBPATH->text());
	settings.setValue("ui/lineEdit_CHPATH",ui->lineEdit_CHPATH->text());
	settings.setValue("ui/lineEdit_pythonPath",ui->lineEdit_pythonPath->text());
	settings.setValue("ui/lineEdit_pic",ui->lineEdit_pic->text());
	settings.setValue("ui/lineEdit_TFeatureDir",ui->lineEdit_TFeatureDir->text());
	settings.setValue("ui/spinBox_sps",ui->spinBox_sps->value());
	settings.setValue("ui/spinBox_start",ui->spinBox_start->value());
	settings.setValue("ui/checkBox_makeChar",ui->checkBox_makeChar->isChecked()?1:0);
}
void DialogMC::loadIni()
{
	QSettings settings(QCoreApplication::applicationFilePath()+".ini",QSettings::IniFormat);
	QString lineEdit_NNPath = settings.value("ui/lineEdit_NNPath",ui->lineEdit_NNPath->text()).toString();
	QString lineEdit_TBPATH = settings.value("ui/lineEdit_TBPATH",ui->lineEdit_TBPATH->text()).toString();
	QString lineEdit_CHPATH = settings.value("ui/lineEdit_CHPATH",ui->lineEdit_CHPATH->text()).toString();
	QString lineEdit_pythonPath = settings.value("ui/lineEdit_pythonPath",ui->lineEdit_pythonPath->text()).toString();
	QString lineEdit_pic = settings.value("ui/lineEdit_pic",ui->lineEdit_pic->text()).toString();
	QString lineEdit_TFeatureDir = settings.value("ui/lineEdit_TFeatureDir",ui->lineEdit_TFeatureDir->text()).toString();
	int spinBox_sps =  settings.value("ui/spinBox_sps",ui->spinBox_sps->value()).toInt();
	int spinBox_start =  settings.value("ui/spinBox_start",ui->spinBox_start->value()).toInt();
	int checkBox_makeChar = settings.value("ui/checkBox_makeChar",ui->checkBox_makeChar->isChecked()?1:0).toInt();
	//fill current Project default
	ui->lineEdit_NNPath->setText(lineEdit_NNPath);
	ui->lineEdit_TBPATH->setText(lineEdit_TBPATH);
	ui->lineEdit_CHPATH->setText(lineEdit_CHPATH);
	ui->lineEdit_pythonPath->setText(lineEdit_pythonPath);
	ui->lineEdit_pic->setText(lineEdit_pic);
	ui->lineEdit_TFeatureDir->setText(lineEdit_TFeatureDir);
	ui->spinBox_sps->setValue(spinBox_sps);
	ui->spinBox_start->setValue(spinBox_start);
	ui->checkBox_makeChar->setChecked(checkBox_makeChar?true:false);

}
QString DialogMC::getFilePath(QString title,QString prefix, QString filter, bool bOpen)
{
	QSettings settings(QCoreApplication::applicationFilePath()+".ini",QSettings::IniFormat);
	QString lastFolder = settings.value(QString("history/%1").arg(prefix),"./").toString();
	QString fileName;
	if (bOpen==false)
		fileName = QFileDialog::getSaveFileName(this,title,lastFolder,filter);
	else
		fileName = QFileDialog::getOpenFileName(this,title,lastFolder,filter);
	if (fileName.size())
	{
		QFileInfo info(fileName);
		settings.setValue(QString("history/%1").arg(prefix),info.absolutePath());
	}
	return fileName;
}


void DialogMC::slot_readyReadOutput()
{
	ui->plainTextEdit_output->appendPlainText(
				QString::fromStdString(m_process->readAllStandardOutput().toStdString())
				);

}
void DialogMC::slot_readyReadErr()
{
	ui->plainTextEdit_output->appendPlainText(
				QString::fromStdString(m_process->readAllStandardError().toStdString())
				);
}
void DialogMC::slot_finished(int code)
{
	slot_readyReadOutput();
	slot_readyReadErr();
	ui->plainTextEdit_output->appendPlainText(QString("\nprogram returns %1\n")
											  .arg(code)
											  );

}
void DialogMC::slot_started()
{

}

